=========================================================#前台jsp页面:var E = window.wangEditor;var editor = new E('#editor')editor.customConfig.uploadImgServer = '${basePath }/ajax/upLoad';// 或者 var editor = new E( document.getElementById('#editor') )editor.create();
后台:@RequestMapping("ajax/upLoad") @ResponseBody public Map upLoad(HttpServletRequest request, HttpServletResponse response) { //创建一个通用的多部分解析器CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());Map map = new HashMap(); //判断 request 是否有文件上传,即多部分请求if(multipartResolver.isMultipart(request)){//转换成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;//取得request中的所有文件名Iterator iter = multiRequest.getFileNames();List data = new ArrayList(); while(iter.hasNext()){ //取得上传文件MultipartFile file = multiRequest.getFile(iter.next());if(file != null){//取得当前上传文件的文件名称String myFileName = file.getOriginalFilename();//如果名称不为“”,说明该文件存在,否则说明该文件不存在if(myFileName.trim() !=""){System.out.println(myFileName);//重命名上传后的文件名String fileName = "/../upload/" + file.getOriginalFilename();//定义上传路径String appRoot = request.getSession().getServletContext().getRealPath("") + File.separator; String path = appRoot + fileName;File localFile = new File(path); if(!localFile.exists()) { localFile.mkdirs(); } try { file.transferTo(localFile); data.add(fileName); } catch (Exception e) { e.printStackTrace(); map.put("errno", 1); map.put("data", data); return map; }}}}map.put("data", data); map.put("errno", 0); }return map; }注意:map.put("data", data);中data里的图片路径必须是正确的,在浏览器页面可以访问的到,不然即使图片上传到本地成功,页面里的富文本编辑器还是没有显示图片的。附上我上传后处理的图片路径:img src="/../upload/Save-Rock-And-Roll.jpg" style="max-width:100%;"